package com.cqc.okokhttpdemo02.http;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;

/**
 * Created by ${cqc} on 2016/11/14.
 * 请求网络数据
 */

public class UserRequest {
    private static UserRequest userRequest;
    private OkHttpClient http;

    private UserRequest() {
        super();
        http = new OkHttpClient();
    }

    public static UserRequest getInstance() {

        if (userRequest == null) {
            userRequest = new UserRequest();
        }
        return userRequest;
    }

    //get请求 不带参数

    // 同步get
    public void syncGet(String name, String pwd) {
        String url = "http://192.168.1.11:8080/okhttp/json1";
        RequestTag tag = RequestTag.GET1;
        Request request = new Request.Builder().url(url).get().build();
        Call call = http.newCall(request);
        MainRequest.getInstance().makeSyncGetRequest(call, tag);
    }

    //异步get
    public void AsyncGet(String name, String pwd) {
        String url = "http://192.168.1.11:8080/okhttp/json2";
        RequestTag tag = RequestTag.GET2;
        Request request = new Request.Builder().url(url).get().build();
        Call call = http.newCall(request);
        MainRequest.getInstance().makeAsyncGetRequest(call, tag);
    }

    //同步post
    public void syncPost(String name, String pwd) {
        String url = "http://192.168.1.11:8080/okhttp/json3";
        RequestTag tag = RequestTag.POST1;
        FormBody formBody = new FormBody.Builder().add("name", name).add("pwd", pwd).build();
        Request request = new Request.Builder().post(formBody).url(url).build();
        Call call = http.newCall(request);
        MainRequest.getInstance().makeSyncPostRequest(call, tag);
    }

    //异步post
    public void AsyncPost(String name, String pwd) {
        String url = "http://192.168.1.11:8080/okhttp/json4";
        RequestTag tag = RequestTag.POST2;
        FormBody formBody = new FormBody.Builder().add("name", name).add("pwd", pwd).build();
        Request request = new Request.Builder().url(url).post(formBody).build();
        Call call = http.newCall(request);
        MainRequest.getInstance().makeAsyncPostRequest(call, tag);
    }
}
